package com.axend.aerosense.room.listener;

import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.axend.aerosense.base.utils.DisplayUtils;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.ui.adapter.RoomEditTouchAdapter;
import com.axend.aerosense.room.ui.view.SlideItemView;

public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private RoomEditTouchAdapter adapter;

    public ItemTouchHelperCallback(RoomEditTouchAdapter adapter) {
        this.adapter = adapter;
    }

    /**
     * 指定可以支持的拖放和滑动的方向，上下为拖动（drag），左右为滑动（swipe）
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
    }

    /**
     * 滑动操作
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (recyclerView == null) return false;

        //如果是滑出来的状态，不移动
        if (viewHolder.itemView instanceof LinearLayout){
            View child = ((LinearLayout) viewHolder.itemView).getChildAt(0);
            if (child != null && child instanceof SlideItemView){
                if (((SlideItemView) child).isOut()) return false;
            }
        }

        int fromPosition = viewHolder.getAdapterPosition();
        int toPosition = target.getAdapterPosition();
        adapter.onItemMove(fromPosition, toPosition);
        return true;
    }

    /**
     * 删掉操作
     */
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        // adapter.onItemDismiss(viewHolder.getAdapterPosition());
    }

    @Override
    public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if (viewHolder == null) return;

//        viewHolder.itemView.setBackground(viewHolder.itemView.getContext().getDrawable(R.drawable.room_item_edit_shadow));
        viewHolder.itemView.setElevation(DisplayUtils.dip2px(5));
        viewHolder.itemView.setOutlineProvider(ViewOutlineProvider.BOUNDS);
        viewHolder.itemView.setTranslationZ(DisplayUtils.dip2px(10));
        viewHolder.itemView.setBackgroundColor(viewHolder.itemView.getContext().getColor(R.color.common_setting_item_bg));
    }

    @Override
    public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        if (viewHolder == null) return;

//        viewHolder.itemView.setBackground(viewHolder.itemView.getContext().getDrawable(R.drawable.room_item_edit_shadow_normal));
        viewHolder.itemView.setElevation(0);
        viewHolder.itemView.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        viewHolder.itemView.setTranslationZ(0);
        viewHolder.itemView.setBackgroundColor(viewHolder.itemView.getContext().getColor(R.color.white));
    }
}
